import dir from '../../assets/images/dir.jpg';
import up from '../../assets/images/up.jpg';
import up_active from '../../assets/images/up_active.jpg';
import info from '../../assets/images/info.jpg';
import info_active from '../../assets/images/info_active.jpg';
import user from '../../assets/images/user.jpg';
import user_active from '../../assets/images/user_active.jpg';
import down from '../../assets/images/down.jpg';
import down_active from '../../assets/images/down_active.jpg';
import { getFileAndFolder, addUpload } from '../../api/index.js'
import { dateFormat } from '../../utils/index.js'
Page({
  data() {
    return {
      up,
      up_active,
      info,
      info_active,
      user,
      user_active,
      down,
      down_active,
      diskName: '',

      btnStyle: {
        'border-radius': '20px',
        'height': '30px',
        'font-size': '14px',
        'line-height': '30px',
        'color': '#FFFFFF',
        'text-shadow': '0px 1px 2px rgba(0, 0, 0, .1)',
        'border': 'none',
        'width': '30%',
        'background': '#3069DC'
      },
      btnDiskPath: {
        'border-radius': '20px',
        'height': '26px',
        'font-size': '14px',
        'line-height': '26px',
        'color': '#000',
        'width': '16%',
        'border': '1px solid #999',

        'background': '#fff'
      },
      showDiskPathDialog: false,
      pathes: [{ name: "全部文件", id: '' }],
      diskFolderList: [],
      fileAndFolderId: '',
      showfileDialog: false,
      folderPathes: [{ name: "我的应用 / 多云管家", id: '' }],
      tiYifolderId: '',
      tiYiList: [],
      tiYiSelectedList: [],
      loading:false
    }
  },
  methods: {
    changeBack(val) {
      console.log("选中的安妮===>", val);
    },
    async confirmFile() {
      let optInfos = this.tiYiSelectedList.map(val => {
        let file = {
          optFileId: val.id,
          optFileName: val.name,
          optParentFileFolderId: val.parentId,
          optParentFileFolderName: val.parentIdName,
          optType: !val.fileType ? '20' : '10'
        }
        if(val.fileType){
          file.size = val.size
        }
        return file
      })
      // console.log(this.pathes)

      let pathes = this.pathes[this.pathes.length - 1]
      let distInfo = {
        optDiskType: 1,
        distDiskType: this.panType,
        distFile: pathes.id,
        distFileName: pathes.id === '' ? '' : pathes.name,
        parentDistFile: pathes.parentId,
        parentDistFileName: pathes.parentIdName,
      }
      


      const { data } = await addUpload({
        optInfos,
        distInfo
      })
      if (data.code === 500&&data.msg==='用户登录已超时，请重新登录') {
        ok.clearH5AppStorage().then(res=>{
          this.$router.replace('/pages/login/index')
        })
        .catch(err=> {
          console.log("接口调用异常",err);
        });
        return
      }
      if(data.code===0){
        this.showfileDialog = false
        this.$Modal.confirm({
          title: '任务创建成功',
          content: '关闭小程序不影响传输任务，且可到任务中心查看最新进度情况',
          submitText: '去查看',
          cancelText: '知道了',
          onCancel: () => {
            console.log('cancel')
          },
          onSubmit: () => {
            this.$router.replace({ path: '/pages/download/index' })
          }
        })
       
        
      }
      // console.log('上传完成了+++++',data);
      
    },
    async getTiYiFileAndFolder() {
      this.loading = true
      const { data } = await getFileAndFolder({
        panType: 1,
        fileAndFolderId: this.tiYifolderId
      })
      this.loading = false
      if (data.code === 500&&data.msg==='用户登录已超时，请重新登录') {
        ok.clearH5AppStorage().then(res=>{
          this.$router.replace('/pages/login/index')
        })
        .catch(err=> {
          console.log("接口调用异常",err);
        });
        return
      }
      let folderList = data.data.folderList.map(val => {
        val.time = val.updateTime
        return val
      })
      let fileList = data.data.fileList.map(val => {
        val.time = val.updateTime
        return val
      })
      folderList.sort((a, b) => {
        let atime = new Date(a.time).getTime()
        let btime = new Date(b.time).getTime()
        return btime - atime
      })
      fileList.sort((a, b) => {
        let atime = new Date(a.time).getTime()
        let btime = new Date(b.time).getTime()
        return btime - atime
      })
      this.tiYiList = [...folderList, ...fileList]
      this.tiYiSelectedList = []
    },
    checkChangeValue(val, file) {
      let item = this.tiYiList.find((item) => {
        return item.id === file.id
      })
      item.checkValue = val

      this.tiYiSelectedList = this.tiYiList.filter((item) => {
        return item.checkValue
      })

      console.log(this.tiYiSelectedList);
    },
    selectedFileByTianYiDisk() {
      this.showfileDialog = true
      this.getTiYiFileAndFolder()
    },
    tiYiPathClick(val) {
      let pathes = this.folderPathes
      let cur = pathes[pathes.length - 1]
      if (cur.id == val.id) {
        return
      }
      this.tiYifolderId = val.id
      while (true) {
        let cur = pathes.pop()
        // 说明这是最后一个
        if (cur.id == val.id) {
          pathes.push(cur)
          break
        }
      }
      this.getTiYiFileAndFolder()
    },
    backFilePath() {
      console.log("dianji le ", this.folderPathes.length - 2 < 0);
      if (this.folderPathes.length - 2 < 0) return
      this.tiYiPathClick(this.folderPathes[this.folderPathes.length - 2])
    },
    tiYiFolderClick(item) {
      if (!item.fileType) {
        this.tiYifolderId = item.id
        this.folderPathes.push({
          id: item.id,
          name: item.name
        })
        this.getTiYiFileAndFolder()
      }
    },

    // 自定义目录按钮
    selectedDiskPath() {
      this.showDiskPathDialog = true
      this.getFileAndFolder()
    },

    // 确定目录选择
    confirmPath() {
      this.showDiskPathDialog = false
    },


    // 返回上一个目录
    backPath() {

      if (this.pathes.length - 2 < 0) return
      this.pathClick(this.pathes[this.pathes.length - 2])
    },

    // 面包屑点击
    pathClick(val) {
      let pathes = this.pathes
      let cur = pathes[pathes.length - 1]
      if (cur.id == val.id) {
        return
      }
      this.fileAndFolderId = val.id
      while (true) {
        let cur = pathes.pop()
        // 说明这是最后一个
        if (cur.id == val.id) {
          pathes.push(cur)
          break
        }
      }
      this.getFileAndFolder()
    },

    // 点击目标文件夹
    clickFolder(item) {
    
      this.fileAndFolderId = item.id
      this.pathes.push({
        id: item.id,
        name: item.name,
        parentId:item.parentId,
        parentIdName:item.parentIdName,
      })
      this.getFileAndFolder()
    },

    // 选这目标目录
    async getFileAndFolder() {
      let panType = this.panType
      this.loading = true
      const { data } = await getFileAndFolder({
        panType,
        fileAndFolderId: this.fileAndFolderId
      })
      this.loading = false
      if (data.code === 500&&data.msg==='用户登录已超时，请重新登录') {
        ok.clearH5AppStorage().then(res=>{
          this.$router.replace('/pages/login/index')
        })
        .catch(err=> {
          console.log("接口调用异常",err);
        });
        return
      }

      
      let folderList = data.data.folderList.map(val => {
        val.time = val.updateTime
        return val
      })
      folderList.sort((a, b) => {
        let atime = new Date(a.time).getTime()
        let btime = new Date(b.time).getTime()
        return btime - atime
      })
      this.diskFolderList = folderList
    }

  },
  computed: {
    diskPath() {
      return this.pathes.reduce((a, b) => {
        return a + ' > ' + b.name
      }, '')
    },
    tiYiSelectedListLength() {
      return this.tiYiSelectedList.length
    }
  },
  mounted() {
    ok.getH5AppStorage({
      key: 'panType'
    }).then(res => {
      this.panType = res.data.data
      this.diskName = this.panType === '3' ? '阿里云盘' : '百度网盘'
    }).catch(err => {
      console.log("接口调用异常", err);
    });
  }
});
